const goodsService = require('../services/goodsService');

const getGoods = async (req, res) => {
  try {
    const goods = await goodsService.getGoods(req.query);
    res.status(200).json(goods);
  } catch (error) {
    res.status(500).json({ error: error.message });
  }
};

const addGoods = async (req, res) => {
  try {
    const newGoods = await goodsService.addGoods(req.body);
    res.status(201).json(newGoods);
  } catch (error) {
    res.status(500).json({ error: error.message });
  }
};

const updateGoods = async (req, res) => {
  try {
    const updatedGoods = await goodsService.updateGoods(req.params.id, req.body);
    res.status(200).json(updatedGoods);
  } catch (error) {
    res.status(500).json({ error: error.message });
  }
};

const deleteGoods = async (req, res) => {
  try {
    await goodsService.deleteGoods(req.params.id);
    res.status(200).json({ message: `Good deleted with ID: ${req.params.id}` });
  } catch (error) {
    res.status(500).json({ error: error.message });
  }
};

const getSuggestions = async (req, res) => {
  try {
    const suggestions = await goodsService.getSuggestions(req.query);
    res.status(200).json(suggestions);
  } catch (error) {
    res.status(500).json({ error: error.message });
  }
};

module.exports = {
  getGoods,
  addGoods,
  updateGoods,
  deleteGoods,
  getSuggestions,
};
